﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using LayerEntity.Persona ;
using LayerEntity.Sistema;

namespace System2012.Persona
{
    public partial class BuscarPersona : DevExpress.XtraEditors.XtraForm 
    {
        #region Variables y propiedades
        EnumTipoPersona tipo;
        bool est = false;
        IEPersona perSeleccionada = null;
        bool serachIsLocal = true;

        public bool Estado
        {
            get
            {
                return this.est;
            }
        }

        public IEPersona Persona
        {
            get
            {
                return this.perSeleccionada ;
            }
        }

        public int IdAddressSelected { get; private set; }
        #endregion
        
        public BuscarPersona(EnumTipoPersona per)
        {
            this.InitializeComponent();
            this.tipo = per;
        }

        private void BuscarPersona_Load(object sender, EventArgs e)
        {
            this.CbTipoBuscar.Properties.DataSource = SystemUtils.RecordManagerPersona.GetValues(this.tipo);
            this.CbTipoBuscar.EditValue = "rs_persona";
          
            this.BGWLoadClientesIncial.RunWorkerAsync();
            this.TxtBuscar.Focus();
        }

        private void SimpleButton1Click(object sender, EventArgs e)
        {
            if (this.CbTipoBuscar.EditValue == null)
            {
                SystemUtils.AlerMessaje("Seleccione un Tipo de Busqueda");
                this.CbTipoBuscar.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.TxtBuscar.EditValue.ToString().Trim()))
            {
                SystemUtils.AlerMessaje("Ingrese datos a buscara");
                this.TxtBuscar.Focus();
                return;
            }

            string concad = string.Format(" and {0} ILIKE '%{1}%'", this.CbTipoBuscar.EditValue.ToString(), this.TxtBuscar.EditValue.ToString());

            if (!this.BGWLoadClientesDB.IsBusy)
            {
                this.BGWLoadClientesDB.RunWorkerAsync(concad);
            }
        }

        #region Procesos BackGround
                
        private void BgwLoadClientesIncialDoWork(object sender, DoWorkEventArgs e)
        {
            this.BGWLoadClientesIncial.ReportProgress(30, "Load Clientes");            
            SystemUtils.RecordManagerPersona.GetClient(100);            
            this.BGWLoadClientesIncial.ReportProgress(100, "Load Complete");
        }

        private void BgwLoadClientesIncialProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.lblProgress.Text = e.UserState.ToString();
            this.progressBarControl1.EditValue = e.ProgressPercentage ;
        }

        private void BgwLoadClientesIncialRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            //   this.gridControl1.DataSource = SystemUtils.RecordManagerPersona.getl
            this.gridControl1.DataSource = SystemUtils.RecordManagerPersona.GetClient();
            this.gridControl1.RefreshDataSource();
        }

        private void BgwLoadClientesDBDoWork(object sender, DoWorkEventArgs e)
        {
            // EPersona obj = (EPersona)SystemUtils.RecordManagerPersona.GetClient(50, e.Argument.ToString());
            this.BGWLoadClientesDB.ReportProgress(30, "Load Clientes");
            e.Result = SystemUtils.RecordManagerPersona.GetClient(50, e.Argument.ToString());
            this.BGWLoadClientesDB.ReportProgress(100, "Load Complete");
        }

        private void BgwLoadClientesDBProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.lblProgress.Text = e.UserState.ToString();
            this.progressBarControl1.EditValue = e.ProgressPercentage;
        }

        private void BgwLoadClientesDBRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.gridControl1.DataSource = e.Result;
            this.serachIsLocal = false;
            this.gridControl1.RefreshDataSource();
            //   gridControl1.Focus();
        }

        #endregion

        private void GridView1Click(object sender, EventArgs e)
        {
            this.GetAddress();
        }

        void GetAddress()
        {
            IEPersona pers = (IEPersona)this.gridView1.GetFocusedRow();
            if (pers != null)
            {
                this.gridControl2.DataSource = pers.GetAddress();
                this.gridControl2.RefreshDataSource();
                this.perSeleccionada = pers;
            }
            else
            {
                this.gridControl2.DataSource = new System.Collections.Generic.List<EDireccion>();
                this.gridControl2.RefreshDataSource();
            }
        }

        private void GridView1CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name == "gcTipoDocumento")
            { 
                object obj = this.gridView1.GetRowCellValue(e.RowHandle, this.gcIdTipoDocumento);
                if (obj != null)
                {
                    EDocumento doc = SystemUtils.RecordManagerCommonList.GetDocument(Convert.ToInt64(obj.ToString()));
                    if (doc != null)
                    {
                        e.DisplayText = doc.Abreviatura ;
                    }
                }
            }
           
            if (e.Column.Name == "gcEstado")
            {
                object obj = this.gridView1.GetRowCellValue(e.RowHandle, this.gcIdEstado);
                if (obj != null)
                {
                    EEstado est = SystemUtils.RecordManagerCommonList.GetEstadoById(Convert.ToInt32(obj.ToString()));
                    if (est != null)
                    {
                        e.DisplayText = est.Descripcion;
                    }
                }
            }
        }

        private void GridView2CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name == "gcEstadoDir")
            {
                object obj = this.gridView2.GetRowCellValue(e.RowHandle, this.gcIdEstadoDir);
                if (obj != null)
                {
                    EEstado est = SystemUtils.RecordManagerCommonList.GetEstadoById((int)obj);
                    if (est != null)
                    {
                        e.DisplayText = est.Descripcion;
                    }
                }
            }

            if (e.Column.Name == "gcDesUbigeo")
            {
                object obj = this.gridView2.GetRowCellValue(e.RowHandle, this.gcUbigeo);
                if (obj != null)
                {
                    EUbigeo ubi = SystemUtils.RecordManagerCommonList.GetUbigeoByUbigeo(obj.ToString());
                    if (ubi != null)
                    {
                        e.DisplayText = ubi.ToString();
                    }
                }
            }
        }

        private void GridView2DoubleClick(object sender, EventArgs e)
        {
            this.SeleccionarDireccion();
        }

        void SeleccionarDireccion()
        {
            EDireccion dir = (EDireccion)this.gridView2.GetFocusedRow();
            if (dir != null)
            {
                this.IdAddressSelected = dir.Item ;
                this.est = true;
                this.Close();
            }
        }

        private void GridView1FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            this.GetAddress();
        }

        private void CambiandoTexto(object sender, EventArgs e)
        {
        }

        private void TxtBuscar_TextChanged(object sender, EventArgs e)
        {
            string concad = string.Format(" and {0} ILIKE '%{1}%'", this.CbTipoBuscar.EditValue, this.TxtBuscar.EditValue);
            if (!this.BGWLoadClientesDB.IsBusy)
            {
                this.BGWLoadClientesDB.RunWorkerAsync(concad);
            }
        }

        private void TxtBuscar_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (this.gridView1.RowCount > 0)
                {
                    //if (this.dtbPersona.Rows.Count > 0)
                    // {
                    this.gridControl1.Focus();
                    //}
                }
            }
        }

        private void TxtBuscar_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (this.gridView1.RowCount > 0)
                {
                    //if (this.dtbPersona.Rows.Count > 0)
                    //{
                    this.gridControl1.Focus();
                    //   }
                }
            }
            if (e.KeyCode == Keys.Up)
            {
                this.TxtBuscar.Focus();
            }
        }

        private void GridView2KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.SeleccionarDireccion();
            }
        }

        private void GridView1KeyPress(object sender, KeyPressEventArgs e)
        {
            if (this.gridView2.RowCount == 1)
            {
                this.SeleccionarDireccion();
            }
            else
            {
                if (this.gridView2.RowCount > 1)
                {
                    this.SeleccionarDireccion();
                }
            }
        }

        private void CbTipoBuscar_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.TxtBuscar.Focus();
            }
        }

        private void CbTipoBuscar_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right)
            {
                this.TxtBuscar.Focus();
            }
        }

        private void BuscarPersona_Shown(object sender, EventArgs e)
        {
            TxtBuscar.Focus();
        }
    }
}